package com.example.wyz.classplus.base;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by 10153 on 2017/5/22.
 */

public abstract class BaseFragment<P extends BasePresenter> extends Fragment {

    protected P mPresenter;
    protected BaseActivity mContext;

    private Unbinder unbinder;

    protected abstract void initPresenter();

    protected abstract int getLayout();

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = (BaseActivity) context;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initPresenter();
        mPresenter.onCreate();
        Log.d("Fragment", "onActivityCreated: ");
    }

    @Override
    public void onStop() {
        super.onStop();
        Log.d(this.getClass().getName(), "onStop: ");
        isPresenterExist("onStop");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        isPresenterExist("onDestroy");
        mPresenter.onDestroy();
    }

    @Override
    public void onDetach() {
        Log.d(this.getClass().getName(), "onDetach: ");
        super.onDetach();
        mContext = null;

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(getLayout(), container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    private void isPresenterExist(String where){
        if(mPresenter != null){
            Log.d(this.getClass().getName(), where+": presenter exist!");
        }
    }

}
